rm(list = ls())

library(data.table)
library(estimatr)

add_backticks = function(x) {
  paste0("`", x, "`")
}

x_lm_formula = function(x) {
  paste(add_backticks(x), collapse = " + ")
}


load('./data/panel_month_dummies_all_2020_months.RData')
panel_all[,covid2:=ifelse(month>'2020-01-01', 1,0)]
panel_all[,covid_econ:=covid2*losing_income_d]
panel_all[,covid_death:=covid2*excess_deaths_jan_d]

panel_all[,time:= as.numeric(round((month-as.Date('2020-01-01',format='%Y-%m-%d'))/(365.25/12)))]

month_cols <-  grep('month_2', colnames(panel_all), value=TRUE)

panel_all[,less_college:=(100-educ_diplBA)]
panel_all[,(paste('educ',month_cols,sep = '_')):= lapply(.SD, function(x) 
  x * panel_all[['less_college']] ), .SDcols = month_cols]

panel_all[,(paste('foreign',month_cols,sep = '_')):= lapply(.SD, function(x) 
  x * panel_all[['foreign_pop']] ), .SDcols = month_cols]

educ_cols <-  grep('educ_month', colnames(panel_all), value=TRUE)
educ_cols <- educ_cols[-157]
foreign_cols <- grep('foreign_month', colnames(panel_all), value=TRUE)
foreign_cols <- foreign_cols[-157]
flex_controls <- c(educ_cols, foreign_cols)

province_cols <- grep('code_province_', colnames(panel_all), value=TRUE)
panel_all[,(paste('trend',province_cols,sep = '_')):= lapply(.SD, function(x) 
  x * panel_all[['time']] ), .SDcols = province_cols]
trend_cols <- grep('trend_code_province', colnames(panel_all), value=TRUE)
trend_cols <- trend_cols[-1]

### Expected unemployment

out_econ <- lm_robust(hc_pc_asians ~ covid_econ, data=panel_all, se_type='stata',
                      clusters = panelvar, fixed_effects = ~ panelvar + month)
save(out_econ, file = './output/out_econ_2020_all.RData')
rm(out_econ)

formula_flex_controls <- as.formula(paste("hc_pc_asians ~ covid_econ +", x_lm_formula(flex_controls)))

out_econ_con <- lm_robust(formula = formula_flex_controls,
                          data=panel_all, se_type='stata',
                          clusters = panelvar, fixed_effects = ~ panelvar + month)

save(out_econ_con, file = './output/out_econ_con_2020_all.RData')

rm(out_econ_con)

panel_all[,trend_econ:=time*losing_income_d]

formula_flex_controls_trend <- as.formula(paste("hc_pc_asians ~ covid_econ + trend_econ +", x_lm_formula(flex_controls)))

out_econ_con_trend <- lm_robust(formula = formula_flex_controls_trend,
                                data=panel_all, se_type='stata',
                                clusters = panelvar, fixed_effects = ~ panelvar + month)

save(out_econ_con_trend, file = './output/out_econ_con_trend_2020_all.RData')

rm(out_econ_con_trend)

flex_controls <- c(educ_cols, foreign_cols, trend_cols)

formula_flex_controls <- as.formula(paste("hc_pc_asians ~ covid_econ + trend_econ +", x_lm_formula(flex_controls)))

out_econ_con_trend_all <- lm_robust(formula = formula_flex_controls,
                                    data=panel_all, se_type='stata',
                                    clusters = panelvar, fixed_effects = ~ panelvar + month)

save(out_econ_con_trend_all, file = './output/out_econ_con_trend_all_2020_all.RData')